//
//  CAVerificationCodeButton.m
//  SCCAAuthSDK
//
//  Created by cyptt on 2020/3/24.
//  Copyright © 2020 com.scca. All rights reserved.
//

#import "CAVerificationCodeButton.h"

@interface CAVerificationCodeButton (){
    int _timeCount;
    int _AllTimeCount;
    BOOL _isStart;
}


@property(nonatomic,strong)NSTimer * timer;
@property(nonatomic,strong)UIColor * defColor;  //默认颜色
@property(nonatomic,strong)UIColor * resetColor; //默认重置颜色
@property(nonatomic,strong)UIColor * timeColor; //倒计时颜色
@end

static int CA_time_count = 60;
@implementation CAVerificationCodeButton






-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
        self.defColor = [UIColor colorWithRed:55/255.f green:141/255.f blue:245/255.f alpha:1];
        self.resetColor = [UIColor colorWithRed:141/255.f green:141/255.f blue:141/255.f alpha:1];
        self.timeColor = [UIColor colorWithRed:84/255.f green:87/255.f blue:94/255.f alpha:1];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self setUp];
}

-(void)setUp{
    
 
  
}

-(void)setCodeParamsFunc:(CodeParams *)codeParams;{
    self.codeParams = codeParams;
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.codeParams.startTitle =   self.codeParams.startTitle?self.codeParams.startTitle:@"获取验证码";
    
    self.codeParams.resetTitle = self.codeParams.resetTitle?self.codeParams.resetTitle:@"重新获取验证码";
    
    self.codeParams.defalutColor = self.codeParams.defalutColor?self.codeParams.defalutColor:self.defColor;
    
    self.codeParams.resetColor = self.codeParams.resetColor?self.codeParams.resetColor:self.resetColor;
    
    self.codeParams.timeColor = self.codeParams.timeColor?self.codeParams.timeColor:self.timeColor;
    self.codeParams.fontFloat =   self.codeParams.fontFloat ?  self.codeParams.fontFloat :13;
    
    if (codeParams.isBorder) {
        self.layer.borderWidth=1;
        self.layer.borderColor = self.codeParams.defalutColor.CGColor;
     
    }
   
    
    [self setTitle: self.codeParams.startTitle  forState:UIControlStateNormal];
    
    [self setTitleColor:self.codeParams.defalutColor forState:UIControlStateNormal];
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    self.titleLabel.font = [UIFont systemFontOfSize:self.codeParams.fontFloat];
    
    [self addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    _AllTimeCount = self.codeParams.code_time_count > 0? self.codeParams.code_time_count:CA_time_count;
    _timeCount = _AllTimeCount;
  
}

-(void)start{
   
    _isStart = YES;
    self.userInteractionEnabled = NO;
    [self btnClick:self];
}


-(void)finish{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    _isStart = NO;
    _timeCount = _AllTimeCount;
    self.userInteractionEnabled = YES;
    [self setTitle: self.codeParams.startTitle  forState:UIControlStateNormal];
    if (self.codeParams.isBorder) {
        self.layer.borderColor = self.codeParams.defalutColor.CGColor;
    }

    [self setTitleColor:self.codeParams.defalutColor forState:UIControlStateNormal];
}
-(void)timeFuc{
    _timeCount --;
    NSString * showTitle = [NSString stringWithFormat:@"%d s",_timeCount];
 
    if ( _timeCount==0) {
        showTitle = self.codeParams.resetTitle;
    
        [self.timer invalidate];
        self.timer = nil;
        
       self.userInteractionEnabled = YES;
           
        if (self.codeParams.isBorder) {
            self.layer.borderColor = self.codeParams.resetColor.CGColor;
        }
      
        [self setTitleColor: self.codeParams.resetColor forState:UIControlStateNormal];
           
            _timeCount = _AllTimeCount;
            
    }
       [self setTitle:showTitle forState:UIControlStateNormal];
 
       
    
}
-(void)btnClick:(UIButton *)sender{
    
  
    //验证
   
    if (!_isStart) {
           return;
    }
    if (_timeCount!= _AllTimeCount) {
        return;
    }
   
    [self captchaSucess];
 
    
}

-(void)captchaSucess{
    if (!self.timer) {
       self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeFuc) userInfo:nil repeats:YES];
    }

    if (self.codeParams.isBorder) {
        self.layer.borderColor = self.codeParams.timeColor.CGColor;
    }
   
    [self setTitleColor: self.codeParams.timeColor forState:UIControlStateNormal];
    NSString * showTitle = [NSString stringWithFormat:@"%d s",_timeCount];
    [self setTitle:showTitle forState:UIControlStateNormal];
    
  
}

@end



@implementation CodeParams



@end
